﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quanlytruongtruongtieuhoc
{
    public partial class frmPhanconggvchunhiem : Form
    {
        public static int hien = 0,kiemtra=0,bien=0,dem=0;
        public static string mlop, mgv;
        public frmPhanconggvchunhiem()
        {
            InitializeComponent();
        }

        private void frmPhanconggvchunhiem_Load(object sender, EventArgs e)
        {
            dem++;
            btLuu.Enabled = false;
            clsKetnoi.taoKetnoi();
            loaddl();
            if (dem == 1)
            {
                dtDanhsachgv.MouseMove += new MouseEventHandler(dtDanhsachgv_MouseMove);
                dtDanhsachlop.DragDrop += new DragEventHandler(dtDanhsachlop_DragDrop);
                dtDanhsachlop.DragEnter += new DragEventHandler(dtDanhsachlop_DragEnter);
            }
            xemPhancong();
        }


        void dtDanhsachlop_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        void dtDanhsachlop_DragDrop(object sender, DragEventArgs e)
        {
            int a = dtDanhsachlop.CurrentRow.Index;
            string kt=dtDanhsachlop.Rows[a].Cells[1].Value.ToString();
            DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
            if (row != null)
            {
                
                    if (kt == "")
                    {
                        dtDanhsachlop.Rows[a].Cells[1].Value = row.Cells[1].Value;
                        kiemtra = 1;
                        bien++;
                        btLuu.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show("Đã xếp giáo viên. ", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    }
            }
        }

        void dtDanhsachgv_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.dtDanhsachgv.DoDragDrop(this.dtDanhsachgv.CurrentRow, DragDropEffects.All);
                    int a = dtDanhsachgv.CurrentCell.RowIndex;
                    if (kiemtra == 1)
                    {
                        dtDanhsachgv.Rows.RemoveAt(a);
                        kiemtra= 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Đã hết giáo viên xếp lớp.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }

        private void xemPhancong()
        {
            try
            {
                SqlDataAdapter ap = new SqlDataAdapter("select a.ma_gv,b.ho,b.ten,a.ma_khoi,a.ma_lop from chu_nhiem a,giao_vien b where a.ma_gv=b.ma_gv", clsKetnoi.con);
                DataTable xem = new DataTable();
                xem.Clear();
                ap.Fill(xem);
                dtKetquacn.Rows.Clear();
                string ten, lop;
                for (int i = 0; i < xem.Rows.Count; i++)
                {
                    DataRow row = xem.Rows[i];
                    dtKetquacn.Rows.Add();
                    ten=(row[1].ToString()+" "+row[2].ToString());
                    lop = (row[3].ToString() + "." + row[4].ToString());
                    dtKetquacn.Rows[i].Cells[0].Value = row[0].ToString();
                    dtKetquacn.Rows[i].Cells[1].Value = ten;
                    dtKetquacn.Rows[i].Cells[2].Value = lop;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void loaddl()
        {
            try
            {
                SqlDataAdapter ap = new SqlDataAdapter("select ma_gv,rtrim(ho),rtrim(ten) from giao_vien where ma_lv='CN' and xoa='False' and ma_gv not in(select ma_gv from chu_nhiem)", clsKetnoi.con);
                DataTable gv = new DataTable();
                gv.Clear();
                ap.Fill(gv);
                string tengv;
                dtDanhsachgv.Rows.Clear();
                for (int i = 0; i < gv.Rows.Count; i++)
                {
                    DataRow row = gv.Rows[i];
                    dtDanhsachgv.Rows.Add();
                    dtDanhsachgv.Rows[i].Cells[0].Value = i + 1;
                    tengv = row[1].ToString()+" " + row[2].ToString();
                    dtDanhsachgv.Rows[i].Cells[1].Value = row[0].ToString();
                    dtDanhsachgv.Rows[i].Cells[2].Value = tengv;
                }
                SqlDataAdapter ap1 = new SqlDataAdapter("select distinct ma_khoi,ma_lop from lop_hoc_sinh where nam_hoc=(select max(nam_hoc) from nam_hoc) except (select ma_khoi,ma_lop from chu_nhiem where nam_hoc=(select max(nam_hoc) from nam_hoc))", clsKetnoi.con);
                DataTable lop = new DataTable();
                lop.Clear();
                ap1.Fill(lop);
                dtDanhsachlop.Rows.Clear();
                string malop;
                for (int j = 0; j < lop.Rows.Count; j++)
                {
                    dtDanhsachlop.Rows.Add();
                    DataRow row1 = lop.Rows[j];
                    malop = (row1[0].ToString() +"." +row1[1].ToString());
                    dtDanhsachlop.Rows[j].Cells[0].Value = malop;
                    dtDanhsachlop.Rows[j].Cells[1].Value = "";
                }
                for (int i = 0; i < dtDanhsachlop.Rows.Count; i++)
                {
                    DataGridViewRow r = dtDanhsachlop.Rows[i];
                    r.Height = 40;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void frmPhanconggvchunhiem_FormClosing(object sender, FormClosingEventArgs e)
        {
            hien = 0;
        }
        public static string layNam()
        {
            SqlCommand com = new SqlCommand("select max(nam_hoc) from nam_hoc", clsKetnoi.con);
            return com.ExecuteScalar().ToString();
        }
        private void btLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult kq = new DialogResult();
                kq = MessageBox.Show("Có chắc lưu không ? ", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (DialogResult.OK == kq)
                {
                    string nam=layNam(),magv,makhoi,malop;
                    for (int i = 0; i < dtDanhsachlop.Rows.Count; i++)
                    {
                        if (dtDanhsachlop.Rows[i].Cells[1].Value.ToString() == "")
                            continue;
                        else
                        {
                            magv=dtDanhsachlop.Rows[i].Cells[1].Value.ToString();
                            makhoi=dtDanhsachlop.Rows[i].Cells[0].Value.ToString().Substring(0,1);
                            malop=dtDanhsachlop.Rows[i].Cells[0].Value.ToString().Substring(2,1);
                            SqlCommand com = new SqlCommand("insert into chu_nhiem values('" + nam + "','" + magv + "','" + makhoi + "','" + malop + "')", clsKetnoi.con);
                            com.ExecuteNonQuery();
                            if (makhoi == "1")
                            {
                                SqlDataAdapter ap = new SqlDataAdapter("select distinct a.ma_mon from mon_hoc_trong_chuong_trinh a, mon_hoc b where ma_ct='1' and ma_lv='CN' and a.ma_mon=b.ma_mon", clsKetnoi.con);
                                DataTable lop1 = new DataTable();
                                ap.Fill(lop1);
                                for (int ii = 0; ii < lop1.Rows.Count; ii++)
                                {
                                    DataRow rowlop1 = lop1.Rows[ii];
                                    SqlCommand comlop1 = new SqlCommand("insert into giang_day values('" + nam + "','" + rowlop1[0].ToString() + "','" + makhoi + "','" + malop + "','"+magv+"')", clsKetnoi.con);
                                    comlop1.ExecuteNonQuery();
                                }
                            }
                            else if (makhoi == "2" || makhoi == "3")
                            {
                                SqlDataAdapter ap1 = new SqlDataAdapter("select distinct a.ma_mon from mon_hoc_trong_chuong_trinh a, mon_hoc b where ma_ct='2' and ma_lv='CN' and a.ma_mon=b.ma_mon", clsKetnoi.con);
                                DataTable lop2 = new DataTable();
                                ap1.Fill(lop2);
                                for (int i2 = 0; i2 < lop2.Rows.Count; i2++)
                                {
                                    DataRow rowlop2 = lop2.Rows[i2];
                                    SqlCommand comlop2 = new SqlCommand("insert into giang_day values('" + nam + "','" + rowlop2[0].ToString() + "','" + makhoi + "','" + malop + "','" + magv + "')", clsKetnoi.con);
                                    comlop2.ExecuteNonQuery();
                                }
                            }
                            else
                            {
                                SqlDataAdapter ap2 = new SqlDataAdapter("select distinct a.ma_mon from mon_hoc_trong_chuong_trinh a, mon_hoc b where ma_ct='3' and ma_lv='CN' and a.ma_mon=b.ma_mon", clsKetnoi.con);
                                DataTable lop3 = new DataTable();
                                ap2.Fill(lop3);
                                for (int i3= 0; i3 < lop3.Rows.Count; i3++)
                                {
                                    DataRow rowlop3 = lop3.Rows[i3];
                                    SqlCommand comlop3 = new SqlCommand("insert into giang_day values('" + nam + "','" + rowlop3[0].ToString() + "','" + makhoi + "','" + malop + "','" + magv + "')", clsKetnoi.con);
                                    comlop3.ExecuteNonQuery();
                                }
                            }
                        }
                    }
                    frmPhanconggvchunhiem_Load(sender, e);
                    MessageBox.Show("Đã lưu. ", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult kq = new DialogResult();
                kq = MessageBox.Show("Có chắc xóa không?", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (DialogResult.OK == kq)
                {
                    string nam=layNam();
                    int a = dtKetquacn.CurrentRow.Index;
                    string ma = dtKetquacn.Rows[a].Cells[0].Value.ToString();
                    SqlCommand com = new SqlCommand("delete from giang_day where ma_gv='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                    com.ExecuteNonQuery();
                    SqlCommand com1 = new SqlCommand("delete from chu_nhiem where ma_gv='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                    com1.ExecuteNonQuery();
                    MessageBox.Show("Đã xóa.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    frmPhanconggvchunhiem_Load(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmIndanhsachchunhiem A = new frmIndanhsachchunhiem();
            A.ShowDialog();
        }

        private void Thoát_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
        }

        private void btCapnhat_Click(object sender, EventArgs e)
        {
            int a=dtKetquacn.CurrentRow.Index;
            mlop = dtKetquacn.Rows[a].Cells[2].Value.ToString();
            mgv = dtKetquacn.Rows[a].Cells[0].Value.ToString();
            frmCapnhatphancongcn A = new frmCapnhatphancongcn();
            A.ShowDialog();
            this.frmPhanconggvchunhiem_Load(sender, e);
        }
    }
}
